package com.ssm.web;

import com.ssm.dao.UserDao;
import com.ssm.entity.Log;
import com.ssm.entity.User;
import com.ssm.service.LogService;
import com.ssm.service.impl.CommonMethod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.List;

/**
 * Created by panbo on 2017/7/28.
 */
@Controller
@RequestMapping("/gotoer")
public class GotoController {
    @Autowired(required = false)
    private UserDao userDao;
    @Autowired(required = false)
    private CommonMethod commonMethod;
    @Autowired
    LogService logService;

    @RequestMapping(value = "/sys/{username}")
    private String gotosys(@PathVariable("username") String username) {
        if (username.equals("admin")){
            return "sysmage";
        }
        return "failtosys";
    }
    @RequestMapping(value = "/file")
    private  String gotofile(HttpServletRequest request,Model model){
        commonMethod.getFileList(request, model);
        return  "welcome";
    }


    @RequestMapping(value = "/exit")
    private  void exitsys(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();

        User user=(User)session.getAttribute("user");
        Log log=new Log();
        log.setUserId(user.getId());
        String opertionstring="用户"+user.getUsername()+"退出了系统 ";
        log.setOperations(opertionstring);
        Timestamp timestamp =new Timestamp(System.currentTimeMillis());
        log.setTime(timestamp);
        logService.addLog(log);

        /*session.invalidate();*/
        request.getRequestDispatcher("/login.jsp").forward(request,response);

    }
    @RequestMapping(value = "/usermag")
    private String gotousermag(HttpServletRequest request) throws Exception {
        List<User> userList = userDao.queryAll();
        if (userList != null) {
            HttpSession session = request.getSession();
            session.setAttribute("userlist", userList);
            return "usermag";
        }
        return "sysmage";
    }






}
